/*
 * Created by JFormDesigner on Fri Nov 01 13:36:39 CST 2024
 */

package com.hafu.view.RuKu;

import com.hafu.model.Gysinfo;
import com.hafu.model.RuKuDetail;
import com.hafu.model.RuKuMain;
import com.hafu.model.SPinfo;
import com.hafu.service.GysinfoService;
import com.hafu.service.RuKuService;
import com.hafu.service.SpInfoService;
import com.hafu.util.StringUtil;

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Vector;

/**
 * @author lenovo
 */
public class AddRuKuFrm extends JFrame {
    private GysinfoService gysinfoService = new GysinfoService();
    private SpInfoService spinfoService = new SpInfoService();
    private RuKuService ruKuService = new RuKuService();
    private TableColumn tableColumn;
    public AddRuKuFrm() {
        initComponents();
        fillComboBox();
    }

    private void fillComboBox() {
        ArrayList<Gysinfo> gysInfoByName = gysinfoService.getGysInfoByName(new Gysinfo());
        for (Gysinfo gysInfo : gysInfoByName) {
            comboBox1.addItem(gysInfo.getName());
        }
        //表格第一列的下拉框
        JComboBox<SPinfo> comboBox = new JComboBox<>();
        ArrayList<SPinfo> spInfoByName = spinfoService.grtSpInfoByName(new SPinfo());
        for (SPinfo spInfo : spInfoByName) {
            comboBox.addItem(spInfo);
        }
        DefaultTableModel defaultTableModel = (DefaultTableModel) table1.getModel();
        tableColumn = table1.getColumnModel().getColumn(0);
        tableColumn.setCellEditor(new DefaultCellEditor(comboBox));
    }

    private void button1(ActionEvent e) {
        // TODO add your code here 给表格添加新行
        DefaultTableModel defaultTableModel = (DefaultTableModel) table1.getModel();
        defaultTableModel.addRow(new Vector());


    }

    private void button2(ActionEvent e) {
        // TODO add your code here 提交数据
        RuKuMain ruKuMain = new RuKuMain();
        ruKuMain.setRuKuName(textField1.getText());
        ruKuMain.setGysName(comboBox1.getSelectedItem().toString());
        //遍历表格每一行
        DefaultTableModel defaultTableModel = (DefaultTableModel) table1.getModel();
        int rowCount = defaultTableModel.getRowCount();
        ArrayList<RuKuDetail> ruKudetailList = new ArrayList<>();
        BigDecimal totalPrice = new BigDecimal(0);
        for (int i = 0; i < rowCount; i++) {
            SPinfo SPinfo1 = (SPinfo)table1.getValueAt(i, 0);
            if (SPinfo1 == null){
                continue;
            }
            if (StringUtil.isEmpty(SPinfo1.getName())){
                continue;
            }
            String s = table1.getValueAt(i, 1).toString();
            Integer num = Integer.parseInt(s);
            RuKuDetail ruKuDetail = new RuKuDetail();
            ruKuDetail.setSpname(SPinfo1.getName());
            ruKuDetail.setNum(num);
            ruKuDetail.setPrice(SPinfo1.getPrice());
            ruKudetailList.add(ruKuDetail);
            totalPrice=totalPrice.add(SPinfo1.getPrice().multiply(new BigDecimal(num)));
        }
        ruKuMain.setRuKuDetailList(ruKudetailList);
        ruKuMain.setPrice(totalPrice);
        boolean b = ruKuService.addRuKu(ruKuMain);
        if (b){
            JOptionPane.showMessageDialog(null, "添加成功");
            this.dispose();
        }else {
            JOptionPane.showMessageDialog(null, "添加失败");
        }

    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        textField1 = new JTextField();
        label2 = new JLabel();
        comboBox1 = new JComboBox();
        button1 = new JButton();
        button2 = new JButton();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();

        //======== this ========
        setTitle("\u6dfb\u52a0\u5165\u5e93\u5355");
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u5165\u5e93\u540d\u79f0");
        contentPane.add(label1);
        label1.setBounds(new Rectangle(new Point(29, 41), label1.getPreferredSize()));
        contentPane.add(textField1);
        textField1.setBounds(175, 35, 80, textField1.getPreferredSize().height);

        //---- label2 ----
        label2.setText("\u4f9b\u5e94\u5546\u9009\u62e9");
        contentPane.add(label2);
        label2.setBounds(new Rectangle(new Point(284, 41), label2.getPreferredSize()));
        contentPane.add(comboBox1);
        comboBox1.setBounds(399, 34, 84, comboBox1.getPreferredSize().height);

        //---- button1 ----
        button1.setText("\u6dfb\u52a0\u65b0\u884c");
        button1.addActionListener(e -> button1(e));
        contentPane.add(button1);
        button1.setBounds(new Rectangle(new Point(534, 34), button1.getPreferredSize()));

        //---- button2 ----
        button2.setText("\u63d0\u4ea4\u5165\u5e93");
        button2.addActionListener(e -> button2(e));
        contentPane.add(button2);
        button2.setBounds(new Rectangle(new Point(691, 34), button2.getPreferredSize()));

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                    new Object[][] {
                            {null, null},
                    },
                    new String[] {
                            "\u5546\u54c1", "\u6570\u91cf"
                    }
            ));
            scrollPane1.setViewportView(table1);
        }
        contentPane.add(scrollPane1);
        scrollPane1.setBounds(105, 130, 645, 140);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JTextField textField1;
    private JLabel label2;
    private JComboBox comboBox1;
    private JButton button1;
    private JButton button2;
    private JScrollPane scrollPane1;
    private JTable table1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
